import axios from 'axios';

export function getOptionData(type, callback) {
    let result = [];
    if (type === "publicData") {
        getPublicData(callback);
    } else if (type === "personalData") {
        getPersonal(callback);
    } else if (type === "all") {
        getAll(callback);
    } else if (type === "addNew") {
        getOptionNew(callback);
    }
    return result;
}

function getPublicData(callback) {
    let result_1 = [];
    axios.get('/data/list?offset=0&size=100')
        .then(function (publicData) {
            let resultList_1 = publicData.data.body.data;
            for (let listData of resultList_1) {
                if (listData !== null) {
                    if (listData.volume !== 0) {
                        let option = {};
                        option.value = listData.data.pk_meta_id;
                        option.label = listData.data.meta_name;
                        result_1.push(option);
                    }
                }
            }
            callback(result_1);
        }).catch(function (error) {
        console.log(error)
    });
}

function getPersonal(callback) {
    let result_2 = [];
    axios.get('/user/data?offset=0&size=100')
        .then(function (personalData) {
            let resultList_1 = personalData.data.body.data;
            for (let listData of resultList_1) {
                if (listData !== null) {
                    if (listData.volume !== 0) {
                        let option = {};
                        option.value = listData.data.id;
                        option.label = listData.data.name;
                        result_2.push(option);
                    }
                }
            }
            callback(result_2);
        }).catch(function (error) {
        console.log(error)
    });
}

function getAll(callback) {
    let pub = [];
    let personal = [];

    function callback_public(res) {
        pub = res;
        callback(pub.concat(personal));
    }

    function callback_personal(res) {
        personal = res;
        getPublicData(callback_public);
    }

    getPersonal(callback_personal);
}

function getOptionNew(callback) {
    let pub = [];
    let personal = [];
    let newOption = [{value: '空气质量指数', label: '空气质量指数'}, {
        value: '平均PM2.5',
        label: '平均PM2.5'
    }, {value: '气温', label: '气温'}, {value: '天气常见参数', label: '天气常见参数'}, {
        value: '污染大气观测指数',
        label: '污染大气观测指数'
    }, {value: '空气相对湿度', label: '空气相对湿度'},];

    function callback_public(res) {
        pub = res;
        callback(pub.concat(personal, newOption));
    }

    function callback_personal(res) {
        personal = res;
        getPublicData(callback_public);
    }

    getPersonal(callback_personal);
}

export function getField(tableId, callback) {
    let result = [];
    if (tableId === '空气质量指数') {
        result = [{field: "Time1", fieldName: "Time"}, {field: "AQI", fieldName: "AQI"}];
        return result;
    } else if (tableId === '气温') {
        result = [{field: "Time", fieldName: "Time"}, {field: "Tem", fieldName: "Tem"}];
        return result;
    } else if (tableId === '平均PM2.5') {
        result = [{field: "Time2", fieldName: "Time"}, {field: "PM", fieldName: "PM"}];
        return result;
    } else if (tableId === '污染大气观测指数') {
        result = [{field: "Kind", fieldName: "Kind"}, {field: "Value", fieldName: "Value"}];
        return result;
    } else if (tableId === '天气常见参数') {
        result = [{field: "天气", fieldName: "天气"}, {field: "风向", fieldName: "风向"}, {field: "气温", fieldName: "气温"}];
        return result;
    } else {
        getPublicDataField(tableId, callback);
    }
}

function getPublicDataField(tableId, callback) {
    let fieldList = [];
    let canSelected = false;
    axios.get('/data/field/' + tableId)
        .then(function (fieldData) {
            let resultList = fieldData.data.body;
            if (resultList.length > 0) {
                for (let listData of resultList) {
                    if (listData !== null) {
                        let option = {};
                        option.field = listData.description;
                        option.fieldName = listData.fieldName;
                        fieldList.push(option);
                    }
                }
            } else {
                canSelected = true;
            }
            callback(fieldList, canSelected);
        }).catch(function (error) {
        console.log(error)
    });
}